package com.xuecheng.content.controller;

import com.xuecheng.api.content.BaseCourseApi;
import com.xuecheng.api.content.model.CourseBaseDTO;
import com.xuecheng.api.content.model.CourseBaseVO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements BaseCourseApi {

    @Autowired
    private CourseBaseService courseBaseService;

//    @Override
//    @GetMapping("/course/all")
//    public List<CourseBase> queryAll(){
//        return courseBaseService.queryAll();
//    }

    @Override
    @PostMapping("/course/list")
    public PageVO queryCouerseList(PageRequestParams page,
                                   @RequestBody QueryCourseModel model) {

        //JWT令牌获取id值
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.queryCourseList(page, model, companyId);
    }

    //实现对课程的新增
    @Override
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //将InsertCourseBserVO转换为Dto类
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.VO2DTO(courseBaseVO);

        //从JWT处获取机构唯一id
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.insertCouerseOne(courseBaseDTO);
    }

    //根据课程id回显所有的数据
    @Override
    @GetMapping(value = "course/{courseBaseId}")
    public CourseBaseDTO queryCourseById(@PathVariable("courseBaseId") Long courseBaseId) {
        //调用工具类获取令牌里的教育机构id值
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.qyeryCourseById(courseBaseId, companyId);

    }

    //修改课程基本信息
    @Override
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //调用工具类获取令牌里的教育机构id值
        Long companyId = SecurityUtil.getCompanyId();
        //将vo数据转换为dto数据
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.VO2DTO(courseBaseVO);
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.modifyCourseBase(courseBaseDTO);
    }

    //根据id实现课程信息逻辑删除
    @Override
    @DeleteMapping("course/{courseBaseId}")
    public void removeCoursebase(@PathVariable Long courseBaseId) {
        //调用工具类从令牌中获取教育机构id
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.removeCourseBase(courseBaseId, companyId);
    }

    //课程提交审核
    @Override
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        //解析令牌获取机构id
        Long companyId = SecurityUtil.getCompanyId();
        //调用service层
        courseBaseService.commitCourseBase(courseBaseId, companyId);

//        return null;
    }


}
